//
//  SeparatePigViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/14.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "SeparatePigViewController.h"
#import "SeparateDetailsViewController.h"
#import "SeparatePigCell.h"
#import "BaseView.h"


@interface SeparatePigViewController ()

@property (nonatomic, strong) UILabel *tipLab;
@property (nonatomic, strong) ShopSeparatePlanModel *model;

@end

@implementation SeparatePigViewController


- (void)setupUI{
    
    {
        self.baseNavView.navTitleLabel.text = @"选择分割方案";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        UIImage *image = [UIImage imageNamed:@"meat_question_nav"];
        self.baseNavView.layer.contents =  (__bridge id)image.CGImage;
    }
    
    {
        [self.view addSubview:self.tipLab];
    }
    
}

- (void)setupLayout
{
    self.reloadBtn.mj_y += 30;
    self.reloadLab.mj_y += 30;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(TJ_NavBarHeight + 34);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - networking methods

- (void)initData{
    
    @weakify(self);
    [[[MeatShopApi sharedInstance] getShopSeparatePlan] subscribeNext:^(ShopSeparatePlanModel * _Nullable model) {
        @strongify(self);
        
        [model.list enumerateObjectsUsingBlock:^(ShopSeparatePlanItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGSize size = [NSString sizeWithHtml:obj.content];
            obj.cellHeight = size.height;
        }];
        
        self.model = model;
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        [self endLoadMoreMoData];

        if (self.model.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}

//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShopSeparatePlanItem *item = self.model.list[indexPath.section];
    SeparateDetailsViewController *vc = [[SeparateDetailsViewController alloc] init];
    vc.item = item;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - tableview.datascource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.model.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 266;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == self.model.list.count - 1 ?0 :10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = KClearColor;
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SeparatePigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeparatePigCell"];
    ShopSeparatePlanItem *item = self.model.list[indexPath.section];
    cell.item = item;
    
    return cell;
}


#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[SeparatePigCell class] forCellReuseIdentifier:@"SeparatePigCell"];

}

- (UILabel *)tipLab{
    
    if (!_tipLab) {
        _tipLab = [UILabel makeLabelWithText:@"请为您的小猪选择分割方案吧～" textColor:UIColorFromRGB(0x666666) font:mFont(14) textAliment:0];
        _tipLab.frame = CGRectMake(15, TJ_NavBarHeight, KScreenWidth - 30, 34);
    }
    return _tipLab;
}

- (ShopSeparatePlanModel *)model{
    
    if (!_model) {
        _model = [[ShopSeparatePlanModel alloc] init];
    }
    return _model;
}

#pragma mark override methods

- (void)loadErrorData
{
    [super loadErrorData];
    self.tipLab.hidden = YES;
}

- (void)loadNoData
{
    [super loadNoData];
    self.tipLab.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.tipLab.hidden = NO;
}

- (void)endRefresh{
    [super endRefresh];
    self.tipLab.hidden = NO;
    
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.tipLab.hidden = NO;
}


@end
